var app=angular.module("ydApp",["ngRoute","ngAnimate"])
	.controller("ydCtrl",["$scope","$http",function($scope,$http){
		$scope.page={pageNum:1};
		$scope.num=0;
	}])
	.directive("star",[function(){
		return {
			restrict:"ECMA",
			template:'<span class=" glyphicon " ng-class="{\'glyphicon-star\':item.type==1,\'glyphicon-star-empty\':item.type==0}" ng-repeat="item in stars track by $index" ng-mouseout="setStar(score)"ng-mouseover="setStar($index+1)" ng-click="setScore($index+1)"></span>',

			link:function(scope,elem,attrs){

				scope.setStar=function(num){
//			    		console.log("out",num);
					scope.stars=[];
					for(var i=1;i<=5;i++){
						var star={};
						if(i<=num){
							star.type=1;
						}else{
							star.type=0;
						}
						scope.stars.push(star);

					}
				}
				scope.setStar(scope.score);
				scope.setScore=function(num){
					scope.score=num;
				}
			},
			scope:{score:"="},

		}

	}])

	.config(["$routeProvider",function($routeProvider){
		$routeProvider
			.when("/home",{"templateUrl":"templates/pages/home.html"})
			.when("/search",{"templateUrl":"templates/pages/search.html"})
			.when("/regist",{"templateUrl":"templates/pages/regist.html"})
			.when("/stadiums/:id",{"templateUrl":"templates/pages/stadiums.html"})
			.otherwise({"redirectTo":"/home"})
	}])
	.controller("homeCtrl",["$scope","$http","$ydsearch","$location",function($scope,$http,$ydsearch,$location){
		$scope.page.pageNum=1;
		$scope.gosearch=function(id){
			$ydsearch.category_id=id;
			$location.path("/search");
		}

	}])
	.controller("searchCtrl",["$scope","$http","$ydsearch",function($scope,$http,$ydsearch){
		$scope.page.pageNum=2;
		$scope.textcont="";
		$scope.provinces=null;
		$scope.citys=[];
		$scope.currentCity={};
		$scope.searchOptions={};
		$scope.stadiums=[];
		$scope.searchInfo={
			city_id:'',//城市id
			district_id:'',//区域id
			start:0,//从第jige
			sort:"1",//排序 0/1
			start_dates:'',//开始日期
			start_time:'',//结束时间
			category_id:'',//场馆类型
			cbd_id:'',//商圈id
			facility_id:'',//条件
			keyword:'' // 搜索关键字
		};	// 当前查询 场馆的条件

		$scope.searchInfo=angular.extend($scope.searchInfo,$ydsearch)
		$http.get("http://platform-api.1yd.me/api/meta-data/cities").success(
			function(res){

				$scope.provinces=res.provinces;
				for (i=0;i<$scope.provinces.length;i++){
					for (j=0;j<$scope.provinces[i].citys.length;j++){
						$scope.citys.push($scope.provinces[i].citys[j])
					}
				}
				$scope.currentCity=$scope.citys[0];
				$scope.getSearchOptions();
				$scope.getStadiums();

			})
		$scope.setCurrentCity=function(item){
			$scope.currentCity=item;
			$scope.searchInfo.city_id=item.city_id;
			$scope.getSearchOptions();
			$scope.getStadiums();

		}

		$scope.getSearchOptions=function(){
			$http.get("http://platform-api.1yd.me/api/meta-data/"+$scope.currentCity.city_id)
				.success(function(res){
					$scope.searchOptions=res;
					console.log($scope.searchOptions);
				})
		}

		// 获取场馆
		$scope.getStadiums=function (){
			$http.get("http://platform-api.1yd.me/api/stadium_resources?"+$.param($scope.searchInfo))
				.success(function(res){
					$scope.stadiums=res;

				})

		}

//	加载更多
//		$scope.loadmovestadium=function(){
//			$scope.searchInfo.start+=10;
//			var bacome=$scope.stadiums;//2步
//			$scope.getStadiums();//从第二步到三步需要时间加载，所有不能及时执行
//			$scope.stadiums=bacome.concat($scope.stadiums);
//
//
//		}

			//加载更多
		$scope.loadmovestadium=function(){
			$scope.searchInfo.start+=10;
			$http.get("http://platform-api.1yd.me/api/stadium_resources?"+$.param($scope.searchInfo))
				.success(function(res){
					$scope.stadiums=$scope.stadiums.concat(res);

				})


		}

//input 内容发生改变其进行搜索
		//$scope.changetext=function(textcont){
		//	alert(textcont)
		//	$scope.getStadiums(textcont)
		//	$scope.stadiums=textcont;
		//}

	}])
	.controller("registCtrl",["$scope","$http",function($scope,$http){
		$scope.page.pageNum=3;
	}])
	.controller("stadiumsCtrl",["$scope","$http","$routeParams","$filter",function($scope,$http,$routeParams,$filter){
		$scope.page.pageNum=2;
		$scope.stadiumsId=$routeParams.id;
		$scope.stadiumInfo=null;		 	// 场馆信息
		$scope.today=new Date();			// 当天的日期
		$scope.currentDay=null;		// 当前选择的日期
		$scope.datelist=[];					// 日期列表
		$scope.stadiumFields=null;			// 场馆的场地列表
		$scope.selectedFields=[];			// 用户选择的场地列表
		$scope.comment=[];
		$scope.getStadiumInfo=function(){
			$http.get("http://platform-api.1yd.me/api/stadiums/"+$scope.stadiumsId).success(
				function(res){
					$scope.stadiumInfo=res;
					console.log($scope.stadiumInfo)

					$http.get("http://platform-api.1yd.me/api/stadiums/"+$scope.stadiumInfo.id+"/comments?size=10&start=0")
						.success(function(res){
							$scope.comment=res;
							console.log($scope.comment);
						})


				}
			)
		}





		$scope.getDateList=function(){
             var ArrayWeek=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
             for (i=0;i<7;i++){
               var obj={};
				 obj.d=new Date($scope.today.getFullYear(),$scope.today.getMonth(),$scope.today.getDate()+i);
				 obj.w=ArrayWeek[obj.d.getDay()]
				 $scope.datelist.push(obj)
				 //console.log($scope.datelist);

			}

			$scope.currentDay=$scope.datelist[0];


		}

		$scope.getStadiumFields=function(){
			var d=$filter('date')($scope.currentDay.d,'yyyy-MM-dd');
			$http.get("http://platform-api.1yd.me/api/stadiums/"+$scope.stadiumsId+"/field_resources?date="+d)
				.success(function(res){
					$scope.stadiumFields=res;
					//console.log($scope.stadiumFields);
				})
		}
		$scope.$watch("stadiumFields",function(){
			$scope.selectedFields=[];
			for(var i=0;i<$scope.stadiumFields.length;i++){
				for(var k=0;k<$scope.stadiumFields[i].field_resource.length;k++){
					if($scope.stadiumFields[i].field_resource[k].resource_status=="selected"){
						$scope.selectedFields.push($scope.stadiumFields[i].field_resource[k]);
					}
				}
			}


		},true)

		$scope.setStatus=function(col,name){
			if(col.resource_status=='ordered'){return}
			if(col.resource_status=='selected'){
				col.resource_status='free';
			}else{
				col.resource_status='selected';
				col.field_name=name;
			}

		}







		$scope.getStadiumInfo();
		$scope.getDateList();
		$scope.getStadiumFields();

	}])

	.factory("$ydsearch",function(){
		return {
			category_id:"",

		}
	})



